<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal">&times;</button>
    <h4 class="modal-title">${change != null ? '修改信息':'添加管理员'}</h4>
</div>
<div class="modal-body">
    <form id="admin-ac" method="post">
        <input type="hidden" name="id" value="${change ? change.id : null}">
        <div  id="error" style="color: red;margin-left: 40%;margin-bottom: 2%;display: none;"></div>
        <div class="form-group">
            <div class="input-group">
                <label class="input-group-addon bg-color-x">账号</label>
                <input name="username" id="username" type="text" class="form-control"
                       value="${change ? change.username : null}" placeholder="管理员账号" onfocus="errorNone();">
            </div>
        </div>
        #if(!change)
        <div class="form-group">
            <div class="input-group">
                <label class="input-group-addon bg-color-x">密码</label>
                <input name="password" id="password" type="text" class="form-control" maxlength="11" placeholder="管理员密码" onfocus="errorNone();">
            </div>
        </div>
        #end
        <div class="form-group">
            <div class="input-group">
                <label class="input-group-addon bg-color-x">称谓</label>
                <input name="name" id="name" type="text" class="form-control"
                       value="${change ? change.name : null}" placeholder="管理员称谓" onfocus="errorNone();">
            </div>
        </div>
        <div class="form-group">
            <div class="input-group">
                <label class="input-group-addon bg-color-x">电话</label>
                <input name="tel" id="tel" type="text" class="form-control"
                       value="${change ? change.tel : null}" maxlength="11"
                       placeholder="管理员联系电话" onfocus="errorNone();">
            </div>
        </div>
    </form>
</div>
<div class="modal-footer">
    <button type="button" class="btn btn-danger" data-dismiss="modal">关闭</button>
    #if (change != null)
    <button id="change" type="submit" class="btn btn-primary">修改</button>
    #else
    <button id="add" type="submit" class="btn btn-info">添加</button>
    #end
</div>
<script>
    // 记录当前修改的用户名，不被判断为占用
    var yName = '${change ? change.username : null}';

    $('#change').click(function (e) {
        e.preventDefault();
        var fd = $('#admin-ac').serialize();
        var tel = checkPhone();
        var name = checkName();
        var username = checkUserame();
        if(username==1&&tel==1&&name==1){
            document.getElementById('error').style.display='none';
        }
        if(document.getElementById('error').style.display=='none'){
            $.post('/admin/change', fd, function (data) {
                if (data.success) {
                    alert("修改成功！");
                    location.reload();
                } else {
                    alert("ERROR: " + data.msg);
                }
            });
        }

    });

    $('#add').click(function (e) {
        e.preventDefault();
        var fd = $('#admin-ac').serialize();
        var tel = checkPhone();
        var name = checkName();
        var password = checkPassword();
        var username = checkUserame();
        if(username==1&&password==1&&tel==1&&name==1){
            document.getElementById('error').style.display='none';
        }
        if(document.getElementById('error').style.display=='none'){
            $.post('/admin/add', fd, function (data) {
                if (data.success) {
                    alert("添加成功！");
                    location.reload();
                } else {
                    alert("ERROR: " + data.msg);
                }
            });
        }

    });

    /**
     * 验证用户名是否被占用
     */
    $('input[name="username"]').blur(function () {
        var self = $(this);
        var username = self.val();
        if (username !== "" && username !== yName) {
            $.post('/admin/check_username', {username: username}, function (data) {
                if (!data.success) {
                    self.val("");
                    alert("ERROR: " + data.msg);
                }
            });
        }
    });

    function checkUserame() {
        var flag = haveChinese($('#username').val());
        if($('#username').val().length<6){
            document.getElementById('error').innerText="用户名不能小于6位！";
            document.getElementById('error').style.display='block';
            return 0;
        }else if(flag == 0){
            document.getElementById('error').innerText="用户名不能存在中文！";
            document.getElementById('error').style.display='block';
            return 0;
        }
        else {
            return 1;
        }
    }
    function haveChinese(ob){
        if (escape(ob).indexOf( "%u" )>=0){
            return 0;
        }else return 1;
    }
    function checkName() {

        if($('#name').val().length==''){
            document.getElementById('error').innerText="称谓不能为空！";
            document.getElementById('error').style.display='block';
            return 0;
        }
        else {
            return 1;
        }
    }

    function checkPassword() {
        if($('#password').val().length<6){
            document.getElementById('error').innerText="密码不能小于6位！";
            document.getElementById('error').style.display='block';
            return 0;
        }
        else {
            return 1;
        }
    }

    function checkPhone() {
        var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;
        if (!myreg.test($('#tel').val())) {
            document.getElementById('error').innerText="手机号输入不正确！";
            document.getElementById('error').style.display='block';
            return 0;
        } else {
            return 1;
        }
    }
    function errorNone(){
        document.getElementById('error').style.display='none';
    }
</script>